package com.and9.mchen.mweather.cw.utils


import groovy.json.JsonException
import groovy.json.JsonSlurper

import java.sql.Timestamp;
import java.text.MessageFormat

import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

import com.and9.mchen.mweather.cw.entity.Weather

final class GJsonDataUtil {

	private static final Log log=LogFactory.getLog(GJsonDataUtil.class);
	
	private GJsonDataUtil(){
		
	} 
	
	private static fetchJsonDataByUrl(String urlString) throws IOException{
		def stream=new URL(urlString).openStream();
		def br=new BufferedReader(new InputStreamReader(stream,"utf-8"));
		def sb=new StringBuilder();
		def temp=null;

		while((temp=br.readLine())!=null){
			sb.append(temp);
		}
		
		temp=sb.toString();
		
		if(temp.startsWith("<!DOCTYPE")){
			return null;
		}
		
		return temp;
	}
	
	def static String getCurrentJsonDataByCityCode(def cityCode){
		def properties=PropertyUtil.getProperties("src/main/resources/url.properties");
		if(properties==null)
			return null;
		try{
			def urlString=MessageFormat.format(properties.get("current.temperature.url"), String.valueOf(cityCode));		
			return fetchJsonDataByUrl(urlString).toString();
		}catch(Exception e){
			log.error("加载出错："+e);
			return null;
		}
	}

	def static String getTodayJsonDataByCityCode(def cityCode){
		def properties=PropertyUtil.getProperties("src/main/resources/url.properties");
		if(properties==null)
			return null;
		try{
			def urlString=MessageFormat.format(properties.get("daily.temperature.url"), String.valueOf(cityCode));
			return fetchJsonDataByUrl(urlString).toString();
		}catch(Exception e){
			log.error("加载出错："+e);
			return null;
		}
	}
	
	def static Weather convertJsonData2DailyWeatherObject(Weather weather, String json){
		def slurper = new JsonSlurper();
		try{
			
			def jWeather = slurper.parseText(json);
			weather.getTemperature().setLow(jWeather?.weatherinfo?.temp1);
			weather.getTemperature().setHeight(jWeather?.weatherinfo?.temp2);
			weather.setWeatherType(jWeather?.weatherinfo?.weather);
			weather.getWeatherImageUrl()[0]=jWeather?.weatherinfo?.img1;
			weather.getWeatherImageUrl()[1]=jWeather?.weatherinfo?.img2;
			weather.setLatelyDailyUpdateTime(new Timestamp(System.currentTimeMillis()));
			weather.setLatelyUpdateTime(weather.getLatelyDailyUpdateTime());
			return weather;
		
		}catch(JsonException e){
			log.error("json转换格式失败："+e);
			return null;
		}catch (NullPointerException e) {
			// TODO: handle exception
			log.error("存在数据为空："+e);
			return null;
		}
		return null;
	}	
	
	def static Weather convertJsonData2CurrentWeatherObject(Weather weather, String json){
		def slurper = new JsonSlurper();
		try{
			def jWeather = slurper.parseText(json);
			weather.getTemperature().setCurrent(jWeather?.weatherinfo?.temp);
			weather.setWindDirection(jWeather?.weatherinfo?.WD);
			weather.setWindSpeed(jWeather?.weatherinfo?.WS);
			weather.setHumidity(jWeather?.weatherinfo?.SD);
			weather.setLatelyRealUpdateTime(new Timestamp(System.currentTimeMillis()));
			weather.setLatelyUpdateTime(weather.getLatelyRealUpdateTime());
			return weather;
		}catch(JsonException e){
			log.error("json转换格式失败："+e);
			return null;
		}catch (NullPointerException e) {
			// TODO: handle exception
			log.error("存在数据为空："+e);
			return null;
		}
		return null;
	}
}
